#include "render.h"
namespace SLG
{
	render::render(void)
	{
	}

	render::~render(void)
	{

	}

	void render::Init()
	{
		for(int i=0;i<MAX;i++)
		{
			m_pVertices[i]=NULL;
		}
		for(int i=0;i<MAX;i++)
		{
			m_pIndex[i]=NULL;
		}
		D3DXCreateSprite(g_pd3dDevice,&m_pSprite); 

	}


	void render::AddWorld(World* w)
	{
		assert(w);
		m_world=w;
	}


	void render::AddMenu(Menu* m)
	{
		assert(m);
		m_menu=m;
	}

	void render::RenderTest()
	{
		D3DXMATRIXA16 matWorld;
		D3DXMatrixIdentity( &matWorld );
		g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
		
		g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		g_pd3dDevice->SetTexture( 0,m_pTexture[TEST]);
	
		g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
		g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );

		g_pd3dDevice->SetRenderState(D3DRS_ALPHAREF,(DWORD)0x000000A5);
		g_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL);
		g_pd3dDevice->SetStreamSource( 0, m_pVertices[TEST], 0, sizeof( CUSTOMVERTEX ) );
		g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
		g_pd3dDevice->SetIndices(m_pIndex[TEST]);
		g_pd3dDevice->DrawIndexedPrimitive(  D3DPT_TRIANGLELIST , 0, 0, 8,0,12);

	}

	void render::AddList(RenderManager* rm)
	{
		m_rManager=rm;
	}

	HRESULT render::Render2DWorld()
	{
		g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		if(SUCCEEDED(m_pSprite->Begin(0)))
		{
			for(vector<PICTURE>::iterator iter=m_menu->m_data.begin();iter<m_menu->m_data.end();iter++)
			{
				if(!iter->draw||iter->tid==CURSOR)
					continue;
				m_pSprite->Draw(iter->tex,NULL,NULL,&D3DXVECTOR3(iter->pos.x,iter->pos.y,0.0f),0xffffffff);
			}
			PICTURE pCursor=m_menu->m_data[m_menu->m_cursor];
			m_pSprite->Draw(pCursor.tex,NULL,NULL,&D3DXVECTOR3(pCursor.pos.x,pCursor.pos.y,0.0f),0xffffffff);
			m_pSprite->End();
		}

		return S_OK;
	}


	/*HRESULT render::RenderMouse(D3DXVECTOR2 p)
	{
		D3DXVECTOR3	position(p.x,p.y,0.0f);
		g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		if(SUCCEEDED(m_pSprite->Begin(0)))
		{
			m_pSprite->Draw(m_pTexture[MOUSE],NULL,NULL,&position,0xffffffff);
			m_pSprite->End();
		}
		// End the scene

		return S_OK;

	}*/

	void render::BeginRender()
	{
		g_pd3dDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
			0xff000000, 1.0f, 0 );
		g_pd3dDevice->BeginScene();
	}

	void render::EndRender()
	{
		g_pd3dDevice->EndScene();
		g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
	}

	void render::TestInit(AABB* aabb)
	{
		if( FAILED( g_pd3dDevice->CreateVertexBuffer( 24 * sizeof( CUSTOMVERTEX ),
			0, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &m_pVertices[TEST], NULL ) ) )
		{
			return ;
		}

		CUSTOMVERTEX* pVertices;

		if( FAILED( m_pVertices[TEST]->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
			return ;
		pVertices[0].position = aabb->m_pos[0];pVertices[0].tu=0.0f;pVertices[0].tv=0.0f;
		pVertices[1].position = aabb->m_pos[4];pVertices[1].tu=0.0f;pVertices[1].tv=1.0f;
		pVertices[2].position = aabb->m_pos[5];pVertices[2].tu=1.0f;pVertices[2].tv=1.0f;
		pVertices[3].position = aabb->m_pos[1];pVertices[3].tu=1.0f;pVertices[3].tv=0.0f;

		pVertices[4].position = aabb->m_pos[3];pVertices[4].tu=0.0f;pVertices[4].tv=0.0f;
		pVertices[5].position = aabb->m_pos[2];pVertices[5].tu=0.0f;pVertices[5].tv=1.0f;
		pVertices[6].position = aabb->m_pos[6];pVertices[6].tu=1.0f;pVertices[6].tv=1.0f;
		pVertices[7].position = aabb->m_pos[7];pVertices[7].tu=1.0f;pVertices[7].tv=0.0f;

		pVertices[8].position = aabb->m_pos[4];pVertices[8].tu=0.0f;pVertices[8].tv=0.0f;
		pVertices[9].position = aabb->m_pos[7];pVertices[9].tu=0.0f;pVertices[9].tv=1.0f;
		pVertices[10].position = aabb->m_pos[6];pVertices[10].tu=1.0f;pVertices[10].tv=1.0f;
		pVertices[11].position = aabb->m_pos[5];pVertices[11].tu=1.0f;pVertices[11].tv=0.0f;

		pVertices[12].position = aabb->m_pos[0];pVertices[12].tu=0.0f;pVertices[12].tv=0.0f;
		pVertices[13].position = aabb->m_pos[1];pVertices[13].tu=0.0f;pVertices[13].tv=1.0f;
		pVertices[14].position = aabb->m_pos[2];pVertices[14].tu=1.0f;pVertices[14].tv=1.0f;
		pVertices[15].position = aabb->m_pos[3];pVertices[15].tu=1.0f;pVertices[15].tv=0.0f;

		pVertices[16].position = aabb->m_pos[3];pVertices[16].tu=0.0f;pVertices[16].tv=0.0f;
		pVertices[17].position = aabb->m_pos[7];pVertices[17].tu=0.0f;pVertices[17].tv=1.0f;
		pVertices[18].position = aabb->m_pos[4];pVertices[18].tu=1.0f;pVertices[18].tv=1.0f;
		pVertices[19].position = aabb->m_pos[0];pVertices[19].tu=1.0f;pVertices[19].tv=0.0f;

		pVertices[20].position = aabb->m_pos[1];pVertices[20].tu=0.0f;pVertices[20].tv=0.0f;
		pVertices[21].position = aabb->m_pos[2];pVertices[21].tu=0.0f;pVertices[21].tv=1.0f;
		pVertices[22].position = aabb->m_pos[6];pVertices[22].tu=1.0f;pVertices[22].tv=1.0f;
		pVertices[23].position = aabb->m_pos[5];pVertices[23].tu=1.0f;pVertices[23].tv=0.0f;

		m_pVertices[TEST]->Unlock();
		if(FAILED(g_pd3dDevice->CreateIndexBuffer(		
			36 * sizeof(WORD),
			D3DUSAGE_WRITEONLY,
			D3DFMT_INDEX16,
			D3DPOOL_MANAGED,
			&m_pIndex[TEST],
			0)))
			return;
		WORD* i = 0;
		m_pIndex[TEST]->Lock(0, 0, (void**)&i, 0);

		i[0] = 0; i[1] = 1; i[2] = 2;
		i[3] = 0; i[4] = 2; i[5] = 3;

		// fill in the back face index data
		i[6] = 4; i[7]  = 5; i[8]  = 6;
		i[9] = 4; i[10] = 6; i[11] = 7;

		// fill in the top face index data
		i[12] = 8; i[13] =  9; i[14] = 10;
		i[15] = 8; i[16] = 10; i[17] = 11;

		// fill in the bottom face index data
		i[18] = 12; i[19] = 13; i[20] = 14;
		i[21] = 12; i[22] = 14; i[23] = 15;

		// fill in the left face index data
		i[24] = 16; i[25] = 17; i[26] = 18;
		i[27] = 16; i[28] = 18; i[29] = 19;

		// fill in the right face index data
		i[30] = 20; i[31] = 21; i[32] = 22;
		i[33] = 20; i[34] = 22; i[35] = 23;

		m_pIndex[TEST]->Unlock();

		if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice,"./Res/test.tga",&m_pTexture[TEST])))
		{
			return ;
		}
	}


	HRESULT render::RenderList(DWORD t)
	{
		m_world->Draw(t);
		return S_OK;
	}
}